<template>
	<view class="register-container">
		<view class="register-box">
			<view class="title">用户注册</view>

			<!-- 用户名输入 -->
			<view class="input-group">
				<view class="input-label">用户名</view>
				<input type="text" v-model="username" placeholder="请设置用户名" class="input" @input="handleInput" />
			</view>

			<!-- 密码输入 -->
			<view class="input-group">
				<view class="input-label">密码</view>
				<input type="password" v-model="password" placeholder="请设置密码" class="input" @input="handleInput" />
			</view>

			<!-- 错误提示 -->
			<view class="error-tip" v-if="errorMsg">{{ errorMsg }}</view>

			<!-- 注册按钮 -->
			<button class="register-btn" @click="register" :disabled="!canSubmit">
				注册
			</button>

			<!-- 返回登录 -->
			<view class="login-link" @click="toLogin">
				已有账号？去登录
			</view>
		</view>
	</view>
</template>

<script>
	import {
		NEW_URL_DEV
	} from "@/common/config.js";
	export default {
		data() {
			return {
				username: '',
				password: '',
				errorMsg: '',
				baseUrl: NEW_URL_DEV + '/api'
			}
		},
		computed: {
			canSubmit() {
				// 简单验证：用户名至少3位，密码至少6位
				return this.username.trim().length >= 3 && this.password.trim().length >= 6
			}
		},
		methods: {
			handleInput() {
				this.errorMsg = ''
			},
			toLogin() {
				uni.navigateBack()
			},
			async register() {
				try {
					uni.showLoading({
						title: '注册中...'
					})

					const {
						data
					} = await uni.request({
						url: `${this.baseUrl}/auth/register`,
						method: 'POST',
						data: {
							username: this.username.trim(),
							password: this.password.trim()
						}
					})

					if (data.code === 200) {
						uni.showToast({
							title: '注册成功'
						})
						// 注册成功后返回登录页
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					} else {
						this.errorMsg = data.msg || '注册失败，请重试'
					}
				} catch (err) {
					this.errorMsg = '网络错误，请检查网络连接'
					console.error('注册失败：', err)
				} finally {
					uni.hideLoading()
				}
			}
		}
	}
</script>

<style scoped>
	.register-container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
		background-color: #f5f7fa;
		padding: 30rpx;
	}

	.register-box {
		width: 100%;
		max-width: 500rpx;
		background-color: #fff;
		padding: 60rpx 40rpx;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		text-align: center;
		margin-bottom: 60rpx;
	}

	.input-group {
		margin-bottom: 40rpx;
	}

	.input-label {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 10rpx;
		display: block;
	}

	.input {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		padding: 0 20rpx;
		border: 2rpx solid #eee;
		border-radius: 10rpx;
		font-size: 30rpx;
		box-sizing: border-box;
	}

	.input:focus {
		border-color: #67c23a;
		outline: none;
	}

	.error-tip {
		color: #f56c6c;
		font-size: 26rpx;
		margin-bottom: 30rpx;
		min-height: 30rpx;
	}

	.register-btn {
		width: 100%;
		height: 88rpx;
		line-height: 88rpx;
		font-size: 32rpx;
		background-color: #67c23a;
		color: #fff;
		border-radius: 10rpx;
		margin-bottom: 20rpx;
	}

	.register-btn:disabled {
		background-color: #b3e19d;
	}

	.login-link {
		text-align: center;
		color: #409eff;
		font-size: 28rpx;
		margin-top: 40rpx;
		cursor: pointer;
	}
</style>